<?php

namespace app\api\controller\v1;

use app\admin\model\BdOrg;
use app\admin\model\Inventory;
use app\admin\model\Material;
use app\admin\model\Materialimage;
use app\admin\model\Surface;
use app\admin\model\Userfavorite;
use think\Cache;

class Favorite extends Base
{

    public function favoriteList()
    {
        $page = $this->params['page'] ?? 1;

        $favorite = Userfavorite::where(['fk_bdl_userid' => $this->user_id, 'forgid' => $this->forgid,'fk_bdl_bd_datacenter'=>$this->datacenter])->column('fk_bdl_inventoryid');
        if(!$favorite)
        {
            $this->success('没有收藏',[]);
        }
        $favorite = array_unique($favorite);


        $cacheTag       = \config('cache_tag')['v1_Favorite_favoriteList'];
        $cacheKey       = $cacheTag. $this->datacenter . $this->forgid . $page . json_encode($favorite);
        $data           =  Cache::get($cacheKey);

        if(!$data){

            //现货库存产品
            $data = Inventory::with(['inventoryentryvalue','mateia','surfaceone','textureone'])
                ->where('fenable', '1')
                ->whereIn('forgid', $this->forgids)
                ->where('fk_bdl_bd_datacenter', $this->datacenter)
                ->where('fk_bdl_usableqty', '>', 0)
                ->order('id', 'desc')
                ->whereIn('id', $favorite)
                ->page($page, $this->pageSize)
                ->select()
                ->each(function($item) {
                    $item->show_mes     = $this->getInventoryStatusMessage($item->fk_bdl_usableqty);
                    //拿到长宽厚
                    $show = [];
                    foreach ($item->inventoryentryvalue as $key => $val)
                    {
                        if(in_array($val['fnumber'],['bdl_thickness','bdl_width','bdl_length','bdl_refthickness','bdl_materialgrade']))
                        {
                            $show[$val['fnumber']] = $val['pivot']['fk_bdl_matpropvalue'];
                        }
                    }


//                    $surface        = Surface::where('fnumber',$item['fk_bdl_bd_surface'])->where('forgid','in',$this->forgids)->find();
//                    $surface_id     = $surface['id'];
                    $surface_id     = $item['surfaceone']['id'];
                    $material_id    = $item['mateia']['id'];
                    if(!empty($surface_id) && !empty($material_id))
                    {
                        $find       = Materialimage::where('fk_bdl_bd_surface',$surface_id)->where('fk_bdl_bd_material',$material_id)->find();
                        if(!empty($find))
                        {
                            $item['show_image'] = $find['fk_bdl_imageurl'];
                        }else{
                            $item['show_image'] = $this->getDefaultImage();
                        }
                    }else{
                        $item['show_image'] = $this->getDefaultImage();
                    }

                    $item->show_matprop = $show;
                });
            Cache::tag($cacheTag)->set($cacheKey,$data,$this->cacheExpire);

        }





        $this->success('获取成功', $data);

    }

    public function actionFavorite()
    {
        $check      = $this->plulicValidate($this->params,
            ['fk_bdl_bd_material|产品ID', 'fk_bdl_inventoryid|库存ID'],
            ['require',  'require']);

        if ($check['code'] == 1)
        {
            $this->error($check['msg']);
        }

        $data = [
            'fk_bdl_bd_material'            => $this->params['fk_bdl_bd_material'],
            'fk_bdl_inventoryid'            => $this->params['fk_bdl_inventoryid'],
            'fk_bdl_userid'                 => $this->user_id,
            'forgid'                        => $this->forgid,
            'fk_bdl_bd_datacenter'          => $this->datacenter
        ];

        //先查找有没有收藏
        $isExits = Userfavorite::where($data)->find();

        if(!$isExits)
        {
            $res    = Userfavorite::create($data);
            $type   = 1;
        }else{
            $res = $isExits->delete();
            $type   = 2;
        }



        if(!$res)
        {
            $this->error('操作失败',$type);
        }

        $cacheTag       = \config('cache_tag')['v1_Favorite_favoriteList'];

        Cache::tag($cacheTag)->clear();

        $this->success('操作成功',$type);
    }




}